package com.zhijiaoyun.service.impl;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhijiaoyun.aliyun.oss.OSSManager;
import com.zhijiaoyun.common.CommonUtils;
import com.zhijiaoyun.common.FileUtils;
import com.zhijiaoyun.dao.ITestDao;
import com.zhijiaoyun.model.Course;
import com.zhijiaoyun.model.News;
import com.zhijiaoyun.model.Test;
import com.zhijiaoyun.model.User;
import com.zhijiaoyun.model.dto.CourseDto;
import com.zhijiaoyun.model.dto.NewsDto;
import com.zhijiaoyun.model.dto.PageList;
import com.zhijiaoyun.service.ICourseService;
import com.zhijiaoyun.service.INewsService;
import com.zhijiaoyun.service.ITestService;
import com.zhijiaoyun.service.IUserService;

/**
 * @author shisfish
 * @date 2017/6/6
 */
@Service(value = "testService")
public class TestServiceImpl implements ITestService {

	@Resource
	ITestDao testDao;

	@Autowired
	ICourseService courseService;
	
	@Autowired
	INewsService newsService;
	
	@Autowired
	IUserService userService;

	@Override
	public List<Test> selectByParams() {
		return testDao.selectByParams();
	}

	@Override
	public void changePicture(String type) throws IOException {
		if ("course".equals(type)) {
			PageList<CourseDto> list = courseService.pageByParamsDto(1, 30, "", "", -1, null);
			if (list != null && list.getList() != null) {
				for (CourseDto courseDto : list.getList()) {
					Course course = courseDto.getCourse();
					String picture = course.getPicture();
					String filePath = "/Users/shisfish/Desktop/course/";
					int index = picture.lastIndexOf("/");
					String fileName = picture.substring(index + 1);
					FileUtils.downLoadFromUrl(picture, fileName, filePath);
					String fileDir = CommonUtils.getProperty("course_picture_path");
					String returnPath = OSSManager.updateFile(fileDir + fileName, filePath + fileName, fileDir, fileName);
					course.setPicture(returnPath);
					courseService.modifyCourse(course);
				}
			}
		} else if ("news".equals(type)) {
			PageList<NewsDto> list = newsService.pageByParamsDto(1, 10, "", null);
			if (list != null && list.getList() != null) {
				for (NewsDto newsDto : list.getList()) {
					News news = newsDto.getNews();
					String picture = news.getImageUrl();
					String filePath = "/Users/shisfish/Desktop/news/";
					int index = picture.lastIndexOf("/");
					String fileName = picture.substring(index + 1);
					FileUtils.downLoadFromUrl(picture, fileName, filePath);
					String fileDir = CommonUtils.getProperty("news_picture_path");
					String returnPath = OSSManager.updateFile(fileDir + fileName, filePath + fileName, fileDir, fileName);
					news.setImageUrl(returnPath);
					newsService.modifyNews(news);
				}
			}
		} else if ("headimg".equals(type)) {
			PageList<User> list = userService.pageByParams(1, 50, "");
			if (list != null && list.getList() != null) {
				for (User user : list.getList()) {
					String picture = user.getHeadImg();
					if (StringUtils.isNotEmpty(picture)) {
						String filePath = "/Users/shisfish/Desktop/headimg/";
						int index = picture.lastIndexOf("/");
						String fileName = picture.substring(index + 1);
						FileUtils.downLoadFromUrl(picture, fileName, filePath);
						String fileDir = CommonUtils.getProperty("user_headimg_path");
						String returnPath = OSSManager.updateFile(fileDir + fileName, filePath + fileName, fileDir, fileName);
						user.setHeadImg(returnPath);
						userService.updataUser(user);
					}
					
				}
			}
		}
	}

}
